package ee.falcon.gui.frame.actionlisteners;

import javax.swing.JList;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import ee.falcon.gui.frame.ProfilesFrame;
import ee.falcon.xml.SpecificProfileInfo;

public class ListSelectionListenerForProfiles implements ListSelectionListener
{
	private final int JLIST_NO_SELECTION_MADE_INDEX = -1; 
	private JList profileNamesList; 
	private ProfilesFrame profilesFrame;
	
	public ListSelectionListenerForProfiles(ProfilesFrame profilesFrame, JList profileNamesList)
	{
		this.profilesFrame = profilesFrame; 
		this.profileNamesList = profileNamesList; 
	}

	@Override
	public void valueChanged(ListSelectionEvent listSelectionEvent) 
	{ 
  	  	if (listSelectionEvent.getValueIsAdjusting() == false) 
	    {
	  	//when there is no selection made in JList
	        if (profileNamesList.getSelectedIndex() == JLIST_NO_SELECTION_MADE_INDEX) 
	        {
		        //No selection, disable fire button.
		         profilesFrame.getEditProfileButton().setEnabled(false); 
		         profilesFrame.getLoginButton().setEnabled(false); 
		         profilesFrame.getDeleteProfileButton().setEnabled(false); 
		         profilesFrame.getCreateNewUserButton().setEnabled(true); 
	
	    	} 
			else 
			{
				profilesFrame.getCreateNewUserButton().setEnabled(false);
				profilesFrame.getEditProfileButton().setEnabled(true); 
				profilesFrame.getLoginButton().setEnabled(true); 
				profilesFrame.getDeleteProfileButton().setEnabled(true); 
	        	
	        	// Get the first (in our case the only) selected item
	        	profilesFrame.setFirstJListSelection((SpecificProfileInfo) profileNamesList.getSelectedValue());
 	        	System.out.println(profilesFrame.getFirstjListSelection().getProtocolEncryptionType());
			}
	    }
  	 }
}
